import { Message } from 'element-ui';
import Cookies from 'js-cookie';
// import variables from '@/styles/variables.scss'
import commonApi from '@/api/common-api';
import request from '../../api/apiConfig';
// export enum DeviceType {
//   Mobile,
//   Desktop,
// }
interface LanguageType {
    code: string;
    languageType: string;
    name: string;
}
export interface IAppState {
    device: string;
    innerHeight: number;
    sidebar: {
        opened: boolean;
        withoutAnimation: boolean;
    };
    language: string;
    size: string;
    isQiankun: boolean;
    LanguageTypeList: LanguageType[];
}
const state: IAppState = {
    sidebar: {
        opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
        withoutAnimation: false
    },
    device: 'desktop',
    language: Cookies.get('language') || 'zh_chs',
    size: Cookies.get('size') || 'medium',
    LanguageTypeList: [],
    isQiankun: false,
    innerHeight: window.innerHeight
};

const mutations = {
    SET_QIANKUN(state: IAppState, isQiankun: boolean) {
        state.isQiankun = isQiankun;
    },
    SETHEIGHT: (state: IAppState, height: number) => {
        state.innerHeight = height;
    },
    TOGGLE_SIDEBAR: (state: IAppState) => {
        state.sidebar.opened = !state.sidebar.opened;
        state.sidebar.withoutAnimation = false;
        if (state.sidebar.opened) {
            Cookies.set('sidebarStatus', 1);
            // variables.sideBarWidth = '180px'
        } else {
            Cookies.set('sidebarStatus', 0);
            // variables.sideBarWidth = '30px'
        }
    },
    CLOSE_SIDEBAR: (state: IAppState, withoutAnimation: boolean) => {
        Cookies.set('sidebarStatus', 0);
        state.sidebar.opened = false;
        state.sidebar.withoutAnimation = withoutAnimation;
    },
    TOGGLE_DEVICE: (state: IAppState, device: string) => {
        state.device = device;
    },
    SET_LANGUAGE: (state: IAppState, language: string) => {
        state.language = language;
        Cookies.set('language', language);
    },
    SET_SIZE: (state: IAppState, size: string) => {
        state.size = size;
        Cookies.set('size', size);
    },
    SET_LANGUAGETYPELIST: (state: IAppState, LanguageTypeList: LanguageType[]) => {
        state.LanguageTypeList = LanguageTypeList;
        /* 应该不需要存cookie*/
        Cookies.set('LanguageTypeList', LanguageTypeList);
    }
};

const actions = {
    // 获取多语言类型
    async LanguageTypeSetting({ commit, state }) {
        const { LanguageTypeList } = state;
        if (LanguageTypeList.length === 0) {
            const res: any = await request.get({
                url: `${commonApi.getLanguages}`
            });
            if (res.code === 0) {
                commit('SET_LANGUAGETYPELIST', res.data);
            } else {
                Message.error('多语言获取失败');
            }
        }
    },
    toggleSideBar({ commit }) {
        commit('TOGGLE_SIDEBAR');
    },
    closeSideBar({ commit }, { withoutAnimation }) {
        commit('CLOSE_SIDEBAR', withoutAnimation);
    },
    toggleDevice({ commit }, device: string) {
        commit('TOGGLE_DEVICE', device);
    },
    setLanguage({ commit }, language: string) {
        commit('SET_LANGUAGE', language);
    },
    setSize({ commit }, size: string) {
        commit('SET_SIZE', size);
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};
